package doInterce.impl;

import cn.hutool.core.convert.Convert;
import doInterce.DoInterface;
import utils.DBUtil;
import utils.PathUtil;
import utils.YmlUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description: 执行数据文件
 * @Author: syh
 * @Date: 2023/12/21/15:15
 */
public class DoExecuteSqlFile implements DoInterface {
    @Override
    public void doExecute() {
        Map<String, Object> ymlMap = YmlUtil.getYmlMap();
        String url = Convert.toStr(ymlMap.get("url"));
        String userName = Convert.toStr(ymlMap.get("userName"));
        String password = Convert.toStr(ymlMap.get("password"));

        String jarPath = PathUtil.getJarPath();
        System.out.println("jarPath ===== "+jarPath);
        try(
                Connection con = DBUtil.getConnection(url,userName,password);
                Statement statement = con.createStatement()
        ){
            jarPath += "sql.sql";

            getSqlContext(new File(jarPath),con,statement);

        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public void getSqlContext(File file,Connection con,Statement statement) throws Exception{
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date startdate = new Date();

        String filePath = "C:\\Users\\Yi\\Desktop\\测试\\sql.txt";


        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));

        String line ;

        AtomicInteger atomicInteger = new AtomicInteger(0);

        String sqlAppender = "";

        boolean skip = false;


        while ((line = bufferedReader.readLine()) != null){
            //去除注释
            if(line.trim().startsWith("--")){//单行注释
                line = line.replaceAll("--.*","");
            }
            line = line.replaceAll("/\\*.*?\\*/","");//多行注释
            byte[] bytes = line.getBytes();
            if(bytes.length == 0){//跳过空行
                continue;
            }
            sqlAppender = sqlAppender + line;
            try {
                //跳过多行注释
                if(line.startsWith("/*")){
                    skip = true;
                    sqlAppender = "";
                    continue;
                }
                if(line.endsWith("*/")){
                    skip = false;
                    sqlAppender = "";
                    continue;
                }
                if(skip){
                    sqlAppender = "";
                    continue;
                }
                if(line.endsWith(");") || line.trim().startsWith("DROP")){//执行sql
                    System.out.println("正在执行====  " +sqlAppender.toString());
                    statement.execute(sqlAppender.toString());
                    atomicInteger.addAndGet(1);
                    sqlAppender = "";
                }
            }catch (Exception e){
                //处理异常的sql
                System.out.println("执行异常的sql："+sqlAppender.toString());
                e.printStackTrace();
                sqlAppender = "";
                return ;
            }
        }

        System.out.println("总条数"+atomicInteger.get());

        Date endDate = new Date();
        System.out.println("执行sql完成"+sdf.format(endDate));
        long milliseconds = endDate.getTime() - startdate.getTime();

        long remainingMilliseconds = milliseconds % (1000 * 60 * 60);
        long minutes = remainingMilliseconds / (1000 * 60);
        System.out.println("耗时毫秒：------------------>"+remainingMilliseconds);
    }
}
